package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Create by lisy on Qingdao
 **/
@RequestMapping("/cart")
@Controller
public class CartController {

    @Autowired
    ICartService iCartService;

    /**
     * ####2.购物车添加商品
     * /cart/add.do
     * http://localhost:8080/cart/add.do?productId=1&count=10
     * 注意这个字段，超过数量会返回这样的标识"limitQuantity"
     * 失败的：LIMIT_NUM_FAIL 成功的：LIMIT_NUM_SUCCESS
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public ServerResponse<CartVo> add(HttpSession session, Integer productId, Integer count){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.add(productId, user.getId(), count);
    }

    /**
     * ####3.更新购物车某个产品数量
     * /cart/update.do
     * http://localhost:8080/cart/update.do?productId=1&count=2
     */
    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse<CartVo> update(HttpSession session, Integer productId, Integer count){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.update(user.getId(), productId, count);
    }

    /**
     * ####4.移除购物车某个产品
     * /cart/delete_product.do
     * http://localhost:8080/cart/delete_product.do?productIds=1,3
     */
    @RequestMapping("delete_product.do")
    @ResponseBody
    public ServerResponse<CartVo> delete(HttpSession session, String productIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.delete(user.getId(), productIds);
    }

    /**
     * ####1.购物车List列表
     * /cart/list.do
     * http://localhost:8080/cart/list.do
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<CartVo> list(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.list(user.getId());
    }

    /**
     * ####5.购物车选中某个商品
     * /cart/select.do
     * http://localhost:8080/cart/select.do?productId=1
     */
    @RequestMapping("select.do")
    @ResponseBody
    public ServerResponse select(HttpSession session, Integer productId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.selectOrUnSelect(user.getId(), productId, Const.Cart.CHECKED);
    }

    /**
     * ####6.购物车取消选中某个商品
     * /cart/un_select.do
     * http://localhost:8080/cart/un_select.do?productId=2
     */
    @RequestMapping("un_select.do")
    @ResponseBody
    public ServerResponse unSelect(HttpSession session, Integer productId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.selectOrUnSelect(user.getId(), productId, Const.Cart.UN_CHECKED);
    }

    /**
     * ####7.查询在购物车里的产品数量
     * /cart/get_cart_product_count.do
     * http://localhost:8080/cart/get_cart_product_count.do
     */
    @RequestMapping("get_cart_product_count.do")
    @ResponseBody
    public ServerResponse getCartProductCount(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createBySuccess(0);
        }
        return iCartService.getCartProductCountService(user.getId());
    }

    /**
     * ####8.购物车全选
     * /cart/select_all.do
     * http://localhost:8080/cart/select_all.do
     */
    @RequestMapping(value = "/select_all.do")
    @ResponseBody
    public ServerResponse<CartVo> selectAll(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.selectOrUnSelect(user.getId(), null, Const.Cart.CHECKED);
    }

    /**
     * ####9.购物车取消全选
     * /cart/un_select_all.do
     * http://localhost:8080/cart/un_select_all.do
     */
    @RequestMapping(value = "/un_select_all.do")
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("未登录");
        }
        return iCartService.selectOrUnSelect(user.getId(), null,  Const.Cart.UN_CHECKED);
    }


}
